package top.melody.ecommerce.webapps.console.controller.security;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import top.melody.ecommerce.common.util.JsonMapper;
import top.melody.ecommerce.common.util.StringUtil;
import top.melody.ecommerce.common.util.page.PageRows;
import top.melody.ecommerce.common.util.page.Pagination;
import top.melody.ecommerce.platform.api.domain.system.SecurityUser;
import top.melody.ecommerce.platform.handler.enums.ResultMsg;
import top.melody.ecommerce.platform.handler.enums.ValidStatus;
import top.melody.ecommerce.webapps.console.base.Constant;
import top.melody.ecommerce.webapps.console.controller.common.BaseController;
import top.melody.ecommerce.webapps.console.weblogic.SecurityUserWeblogic;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

@Controller
@RequestMapping("/security/user")
public class SecurityUserController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(SecurityUserController.class);
    @Resource
    private SecurityUserWeblogic securityUserWeblogic;
    @Resource
    private ResourceBundleMessageSource i18nSource;

    @RequestMapping("/list")
    public ModelAndView queryAccountListByPage(@RequestParam(required = false) String loginName,
                                               @RequestParam(required = false) String nickName,
                                               @RequestParam(value = "pager.offset", required = false, defaultValue = "0")Integer startRowIndex,
                                               @RequestParam(value = "maxPageItems", required = false, defaultValue = "10") Integer pageSize) {
        String logicViewName = "security/user-list";
        Map<String, Object> paramMap = new HashMap<String, Object>();
        try {
            SecurityUser queryEntity = new SecurityUser();
            if (!StringUtil.isEmpty(loginName))
                queryEntity.setOperateUserId(loginName);
            if (!StringUtil.isEmpty(nickName))
                queryEntity.setScreenName(nickName);
            Pagination page = new Pagination(0, startRowIndex / pageSize + 1, pageSize);
            PageRows<SecurityUser> pageRows = securityUserWeblogic.querySecurityUserListByPage(queryEntity, page);
            paramMap.put("loginName", loginName);
            paramMap.put("nickName", nickName);
            paramMap.put("validStatus", ValidStatus.VALID);
            paramMap.put("page", pageRows.getPage());
            paramMap.put("rows", pageRows.getRows());
        } catch (Exception e) {
            logicViewName = "error/500";
            logger.error("query security user list by page error", e);
        }
        return new ModelAndView(logicViewName, paramMap);
    }

    @RequestMapping("/operate")
    @ResponseBody
    public String operateSecurityUser(String securityUserId) {
        ResultMsg resultMsg = new ResultMsg(i18nSource.getMessage(Constant.ERROR_GLOBAL_EXCEPTION, null, Locale.CHINA));
        try {
            SecurityUser user = securityUserWeblogic.getSecurityUser(securityUserId);
            resultMsg.setMsg(user.getValidStatus().getCode());
            if (user.getValidStatus().equals(ValidStatus.LOCKED))
                user.setValidStatus(ValidStatus.VALID);
            else
                user.setValidStatus(ValidStatus.LOCKED);
            securityUserWeblogic.modifySecurityUser(user);
            resultMsg.setStatusCode(ResultMsg.CODE_S);
        } catch (Exception e) {
            logger.error("operate security user failed", e);
        }
        return JsonMapper.nonEmptyMapper().toJson(resultMsg);
    }
}
